cmake_minimum_required(VERSION 3.16)

# Find Qt6Test
find_package(Qt6 REQUIRED COMPONENTS Core Test)

# Enable testing
enable_testing()

# UDM Test
add_executable(test_udm test_udm.cpp
    ../src/core/UniversalDataModel.cpp
    ../src/core/DataPoint.h
)

target_include_directories(test_udm PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/core
)

target_link_libraries(test_udm
    Qt6::Core
    Qt6::Test
)

add_test(NAME UDM_Test COMMAND test_udm)

# Channel Test
add_executable(test_channel test_channel.cpp
    ../src/core/Channel.cpp
    ../src/core/UniversalDataModel.cpp
    ../src/core/DataTypes.cpp
    ../src/adapters/interfaces.cpp
    ../src/adapters/ModbusTcpCollector.cpp
    ../src/adapters/ModbusTcpServer.cpp
    ../src/core/DataPoint.h
    ../src/adapters/interfaces.h
)

target_include_directories(test_channel PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/core
    ${CMAKE_SOURCE_DIR}/src/adapters
    ${MODBUS_INCLUDE_DIR}
)

target_link_libraries(test_channel
    Qt6::Core
    Qt6::Test
    ${MODBUS_LIBRARY}
)

if(WIN32)
    target_link_libraries(test_channel ws2_32)
endif()

add_test(NAME Channel_Test COMMAND test_channel)

# ModbusTcpCollector Test
add_executable(test_modbus_collector test_modbus_collector.cpp
    ../src/adapters/interfaces.cpp
    ../src/adapters/ModbusTcpCollector.cpp
    ../src/core/UniversalDataModel.cpp
    ../src/core/DataTypes.cpp
    ../src/core/DataPoint.h
    ../src/adapters/interfaces.h
)

target_include_directories(test_modbus_collector PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/core
    ${CMAKE_SOURCE_DIR}/src/adapters
    ${MODBUS_INCLUDE_DIR}
)

target_link_libraries(test_modbus_collector
    Qt6::Core
    Qt6::Test
    ${MODBUS_LIBRARY}
)

if(WIN32)
    target_link_libraries(test_modbus_collector ws2_32)
endif()

add_test(NAME ModbusCollector_Test COMMAND test_modbus_collector)

# ModbusTcpServer Test
add_executable(test_modbus_server test_modbus_server.cpp
    ../src/adapters/interfaces.cpp
    ../src/adapters/ModbusTcpServer.cpp
    ../src/core/UniversalDataModel.cpp
    ../src/core/DataTypes.cpp
    ../src/core/DataPoint.h
    ../src/adapters/interfaces.h
)

target_include_directories(test_modbus_server PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/core
    ${CMAKE_SOURCE_DIR}/src/adapters
    ${MODBUS_INCLUDE_DIR}
)

target_link_libraries(test_modbus_server
    Qt6::Core
    Qt6::Test
    ${MODBUS_LIBRARY}
)

if(WIN32)
    target_link_libraries(test_modbus_server ws2_32)
endif()

add_test(NAME ModbusServer_Test COMMAND test_modbus_server)

# DataTypes Test
add_executable(test_datatypes test_datatypes.cpp
    ../src/core/DataTypes.cpp
    ../src/core/DataTypes.h
)

target_include_directories(test_datatypes PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/core
)

target_link_libraries(test_datatypes
    Qt6::Core
    Qt6::Test
)

add_test(NAME DataTypes_Test COMMAND test_datatypes)

# Integration Test
add_executable(test_integration test_integration.cpp
    ../src/core/Channel.cpp
    ../src/core/ChannelManager.cpp
    ../src/core/UniversalDataModel.cpp
    ../src/core/DataTypes.cpp
    ../src/adapters/interfaces.cpp
    ../src/adapters/ModbusTcpCollector.cpp
    ../src/adapters/ModbusTcpServer.cpp
    ../src/utils/ConfigManager.cpp
)

target_include_directories(test_integration PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/core
    ${CMAKE_SOURCE_DIR}/src/adapters
    ${CMAKE_SOURCE_DIR}/src/utils
    ${MODBUS_INCLUDE_DIR}
)

target_link_libraries(test_integration
    Qt6::Core
    Qt6::Test
    ${MODBUS_LIBRARY}
)

if(WIN32)
    target_link_libraries(test_integration ws2_32)
endif()

add_test(NAME Integration_Test COMMAND test_integration)
